package ad6;

import java.io.FileOutputStream;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class CreateXml {

	public static void main(String[] args) {
		// 从无到有，构建一个xml
		// 1：获得Document
		Document doc = DocumentHelper.createDocument();
		// 构建root
		Element root = doc.addElement("classes");
		root.addAttribute("no", "java47");
		Element s1 = root.addElement("student");
		s1.addAttribute("sex", "男");
		s1.addElement("name").setText("黄日天");
		s1.addElement("school").setText("合肥学院");

		// 保存
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("utf-8");
		try {
			XMLWriter xm = new XMLWriter(new FileOutputStream("java47.xml"),
					format);
			xm.write(doc);
			xm.close();
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}
	}

}
